package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.GoalCategory;
import com.igoal.entity.GoalCategoryPk;

@Repository("goalCategoryDAO")
public class GoalCategoryDAOImpl extends
		GenericDaoHibernate<GoalCategory, GoalCategoryPk> implements
		GoalCategoryDAO {

	public GoalCategoryDAOImpl() {
		super(GoalCategory.class);
	}

	public boolean exists(Long goalId, Long categoryId) {
		return findByPk(goalId, categoryId) != null;
	}

	public List<GoalCategory> findByCategory(Long categoryId) {
		DetachedCriteria dc = DetachedCriteria.forClass(GoalCategory.class);
		dc.add(Restrictions.eq("pk.category.id", categoryId));
		getByStatus(dc, DataStatus.USING);
		
		return findByCriteria(dc);		
	}

	public List<GoalCategory> findByGoal(Long goalId) {
		DetachedCriteria dc = DetachedCriteria.forClass(GoalCategory.class);
		dc.add(Restrictions.eq("pk.goal.id", goalId));
		getByStatus(dc, DataStatus.USING);
		
		return findByCriteria(dc);
	}
	
	public GoalCategory findByPk(Long goalId, Long categoryId){
		DetachedCriteria dc = DetachedCriteria.forClass(GoalCategory.class);
		dc.add(Restrictions.eq("pk.category.id", categoryId));
		dc.add(Restrictions.eq("pk.goal.id", goalId));
		getByStatus(dc, DataStatus.USING);
		
		List<GoalCategory> result = findByCriteria(dc);
		if(result.size() > 0)
			return result.get(0);
		
		return null;
	}
}
